/*
 * @Author: fhw
 * @Date: 2022-07-17 13:35:20
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2023-02-04 21:53:30
 * @Description:
 */
import {
  Controller,
  Get,
  UseFilters,
  Query,
  HttpException,
  HttpStatus,
  Body,
  Post,
  HttpCode,
  Headers,
} from '@nestjs/common';
import { AuthGuard } from '@nestjs/passport';
import { ApiBearerAuth, ApiBody, ApiOperation, ApiParam, ApiQuery, ApiResponse, ApiTags } from '@nestjs/swagger';
import { isEmpty, isNil } from 'ramda';
import { ArticleService } from './article.service';
import {
  UpdateArticleThumbsUpDto,
  ArticlePageDto,
  CreateUpdateArticleDto,
  ArticleInfoDto,
} from './dto/article.dto';
import {
  ArticleIdAndTitle,
  ArticleInfo,
  ArticleResult,
} from './entities/article.entity';
import { HttpExceptionFilter } from 'src/common/http-exception.filter';
import { DelteIdDto } from '../admin-user/dto/admin-user.dto';
import { Roles } from 'src/common/auth/role.decorator';
@Controller('article')
@ApiTags('文章')
@ApiBearerAuth() // swagger文档设置token
export class ArticleController {
  constructor(
    private readonly articleService: ArticleService,
  ) { }
  @Get('/getArticlePage')
  @ApiResponse({
    status: 200,
    description: '文章列表',
    type: ArticleResult,
  })
  @ApiOperation({ summary: '文章列表', operationId: 'getArticlePage' })
  @UseFilters(new HttpExceptionFilter())
  getArticlePage(@Query() params: ArticlePageDto) {
    return this.articleService.getArticlePage(params);
  }

  @Get('/getArticleLately')
  @ApiResponse({
    status: 200,
    description: '最新文章',
    type: [ArticleIdAndTitle],
  })
  @ApiOperation({ summary: '最新文章', operationId: 'getArticleLately' })
  getArticleLately() {
    return this.articleService.getArticleLately();
  }

  @Get('/getArticleHot')
  @ApiResponse({
    status: 200,
    description: '热门文章',
    type: [ArticleIdAndTitle],
  })
  @ApiOperation({ summary: '热门文章', operationId: 'getArticleHot' })
  getArticleHot() {
    return this.articleService.getArticleLately();
  }

  @Get('/getArticleInfo')
  @ApiResponse({
    status: 200,
    description: '文章详情',
    type: ArticleInfo,
  })
  @ApiOperation({ summary: '文章详情', operationId: 'getArticleInfo' })
  getArticleInfo(@Query() params: ArticleInfoDto) {
    if (isNil(params.id) || isEmpty(params.id)) {
      throw new HttpException(
        {
          status: HttpStatus.BAD_REQUEST,
          message: '请求参数id必传',
          error: 'id is required',
        },
        HttpStatus.BAD_REQUEST,
      );
    }
    return this.articleService.getArticleInfo(params);
  }

  @HttpCode(200)
  @Post('/updateArticleThumbsUp')
  @ApiQuery({
    description: '更新文章点赞数',
    type: UpdateArticleThumbsUpDto,
  })
  @ApiBody({
    description: '更新文章点赞数',
    type: UpdateArticleThumbsUpDto,
  })
  @ApiOperation({ summary: '更新文章点赞数', operationId: 'updateArticleThumbsUp' })
  updateArticleThumbsUp(@Body() body: UpdateArticleThumbsUpDto) {
    return this.articleService.updateArticleThumbsUp(body);
  }

  @HttpCode(200)
  @Post('/createUpdateArticle')
  @ApiParam({
    name: '创建更新文章',
    description: '创建更新文章',
    type: CreateUpdateArticleDto,
  })
  @ApiBody({
    description: '创建更新文章',
    type: CreateUpdateArticleDto,
  })
  @ApiOperation({ summary: '创建更新文章', operationId: 'createUpdateArticle', })
  @Roles() // 自定义装璜器
  public async createUpdateArticle(@Body() body: CreateUpdateArticleDto, @Headers() headers) {
    return this.articleService.createUpdateArticle(body, headers);
  }
  @HttpCode(200)
  @Post('/deleteArticle')
  @ApiQuery({
    description: '删除文章',
    type: DelteIdDto,
  })
  @ApiBody({ description: '删除文章', type: DelteIdDto })
  @ApiOperation({ summary: '删除文章', operationId: 'deleteArticle' })
  @Roles() // 自定义装璜器
  deleteArticle(@Body() body: DelteIdDto) {
    return this.articleService.deleteArticle(body);
  }

  @HttpCode(200)
  @Post('/updateArticleReadCount')
  @ApiQuery({
    description: '更新文章阅读数',
    type: UpdateArticleThumbsUpDto,
  })
  @ApiBody({
    description: '更新文章阅读数',
    type: UpdateArticleThumbsUpDto,
  })
  @ApiOperation({ summary: '更新文章阅读数', operationId: 'updateArticleReadCount' })
  updateArticleReadCount(@Body() body: UpdateArticleThumbsUpDto) {
    return this.articleService.updateArticleReadCount(body);
  }
}
